<?php
/**
 * Version: 1.0
 *
 * @category    SGN
 * @package     Lib
 * @subpackage	Queue
 * @copyright   Copyright (c) 2010 VNG (http://www.vng.com.vn)
 * @license     http://www.vng.com.vn
 * @version     $Id: Paging.php 1413 2012-02-29 07:10:26Z locth $
 * @author      $Author: locth $
 *
 */
class SGN_Paging
{
    /**
     * The function compute paging
     *
     * @param int $rowTotal    : The total of record to pagging
     * @param int $currentPage    : The number of page activate
     * @param int $itemPerPage  : The number of record item per page
     * @param int $pageNumLimit     : The number of page per block (   )
     * @return array
     */
    public static function calculate( $rowTotal, $currentPage, $itemPerPage, $pageNumLimit )
    {
        //----------------------------------------------
        // init data-returned
        //----------------------------------------------
        $pageInfo = array();

        //----------------------------------------------
        // check input
        //----------------------------------------------
        if( is_numeric( $rowTotal ) == false || $rowTotal < 1 ||
            is_numeric( $itemPerPage ) == false || $itemPerPage < 1 ){
            return $pageInfo;
        }

        //----------------------------------------------
        // correct data
        //----------------------------------------------
        $currentPage = intval( $currentPage );
        if( $currentPage < 1 ){
            $currentPage = 1;
        }
        $pageNumLimit = intval( $pageNumLimit );

        //----------------------------------------------
        // check condition
        //----------------------------------------------
        if( $itemPerPage > 0 && $rowTotal > $itemPerPage ){
            //----------------------------------------------
            // calculate total Page
            //----------------------------------------------
            $totalPage = intval( ceil( $rowTotal / $itemPerPage ) );

            //----------------------------------------------
            // calculate prev & next page
            //----------------------------------------------
            $previous = intval( $currentPage - 1 );
            if( $previous < 1 ){
                $previous = 1;
            }
            $next = intval( $currentPage + 1 );
            if( $next > $totalPage ){
                $next = $totalPage;
            }

            //----------------------------------------------
            // calculate start & end page
            //----------------------------------------------
            if( $pageNumLimit == 0 || $totalPage <= $pageNumLimit ){
                $pageStart = 1;
                $pageEnd   = $totalPage;
            }else{
                //----------------------------------------------
                // compute the distance from current page
                //----------------------------------------------
                $distance = intval( floor( ( $pageNumLimit - 1 ) / 2 ) );

                //----------------------------------------------
                // compute the end page
                //----------------------------------------------
                $pageEnd = intval( $currentPage + $distance );
                if( $pageEnd > $totalPage ){
                    $pageEnd = $totalPage;
                }

                //----------------------------------------------
                // compute the start page
                //----------------------------------------------
                $pageStart = intval( $pageEnd - $pageNumLimit + 1 );
                if( $pageStart < 1 ){
                    $pageStart = 1;
                    //----------------------------------------------
                    // re-compute the end page
                    //----------------------------------------------
                    $pageEnd = intval( $pageStart + $pageNumLimit - 1 );
                    if( $pageEnd > $totalPage ){
                        $pageEnd = $totalPage;
                    }
                }
            }

            //----------------------------------------------
            // return data
            //----------------------------------------------
            $pageInfo["total"]      = $totalPage;
            $pageInfo["current"]    = $currentPage;
            $pageInfo["previous"]   = $previous;
            $pageInfo["next"]       = $next;
            $pageInfo["start"]      = $pageStart;
            $pageInfo["end"]        = $pageEnd;
        }

        //----------------------------------------------
        // return data
        //----------------------------------------------
        return $pageInfo;
    }
}